//
//  BottomButton.swift
//  aj
//
//  Created by leeqb on 2/15/16.
//  Copyright © 2016 powerlee. All rights reserved.
//

import UIKit

@objc protocol BottomButtonDelegate {
    // 按钮点击事件
    func bottomButtonClick(index: Int)
    
    optional func bottomButtonDidShow(buttonView: BottomButton)
    optional func bottomButtonDidDismiss(buttonView: BottomButton)
}

class BottomButton: AJBaseViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    var delegate: BottomButtonDelegate!
    // {"image":"本地图片名称","title":"按钮标题"}
    var buttons: [[String : String]]!
    @IBOutlet weak var collectionView: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.frame = CGRect(x: 0, y: SCREEN_HEIGHT, width: SCREEN_WIDTH, height: 130)
        collectionView.delaysContentTouches = false
    }
    
    class func getInstance() -> BottomButton {
        let instance = LB("Common", vcName: "BottomButton") as! BottomButton
        return instance
    }
    
    //MARK: - UICollectionViewDataSource
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return buttons.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("BottomButtonCell", forIndexPath: indexPath)
        
        if cell.selectedBackgroundView == nil {
            let backgroundView = UIView()
            backgroundView.backgroundColor = UIColor.orangeColor()
            cell.selectedBackgroundView = backgroundView
        }
        
        let imageView = cell.viewWithTag(1) as! UIImageView
        let textLabel = cell.viewWithTag(2) as! UILabel
        
        let buttonData = buttons[indexPath.row]
        imageView.image = UIImage(named: buttonData["image"]!)
        textLabel.text = buttonData["title"]
        
        return cell
    }
    
    //MARK: - UICollectionViewDelegate
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        collectionView.deselectItemAtIndexPath(indexPath, animated: false)
        
        if delegate != nil {
            delegate.bottomButtonClick(indexPath.row)
        }
    }
    
    func show() {
        let keyWindow = UIApplication.sharedApplication().keyWindow
        keyWindow?.addSubview(self.view)
        collectionView.reloadData()
        
        UIView.animateWithDuration(0.3, animations: { () -> Void in
            self.view.frame.origin.y -= 130
            }) { (completion: Bool) -> Void in
                if self.delegate != nil && self.delegate.bottomButtonDidShow != nil {
                    self.delegate.bottomButtonDidShow!(self)
                }
        }
    }
    
    func hide() {
        UIView.animateWithDuration(0.3, animations: { () -> Void in
            self.view.frame.origin.y = SCREEN_HEIGHT
            }) { (completion: Bool) -> Void in
                if self.delegate != nil && self.delegate.bottomButtonDidDismiss != nil {
                    self.delegate.bottomButtonDidDismiss!(self)
                }
                self.view.removeFromSuperview()
        }
    }

    @IBAction func cancelClick(sender: UIButton) {
        self.hide()
    }
}
